package com.agm.android.fragments;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.agm.android.constants.Constants;
import com.agm.dto.Partita;
import com.example.android.navigationdrawerexample.App;
import com.example.android.navigationdrawerexample.R;
import com.fasterxml.jackson.databind.ObjectMapper;

public class GiornataFragment extends Fragment {
	public static final String ARG_PLANET_NUMBER = "planet_number";

	public GiornataFragment() {
		// Empty constructor required for fragment subclasses
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_giornata, container,false);

		CalendarioTask calendarioTask = new CalendarioTask();
		calendarioTask.execute();
		//String[] rows  = st.split("_");
		
		//TableLayout tableLayout = new TableLayout(rootView.getContext());
		ScrollView scrollView = (ScrollView) rootView.findViewById(R.id.scrollgiornata);
        TableLayout tableLayout = (TableLayout)scrollView.findViewById(R.id.tabGiornata);
        tableLayout.removeAllViews();
        try {
			calendarioTask.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//        int counter = App.partite.length / 5;
        int nGiornata = 0;
		//for(int i=0 ; i<counter ; i++){
			
        	TableLayout tableLayoutIntestazione = new TableLayout(rootView.getContext());
			TableRow tableRowIntestazione = new TableRow(rootView.getContext());
			TextView giornata = new TextView(rootView.getContext());
			
			
			TableLayout tableLayoutNew = new TableLayout(rootView.getContext());
			for(int j=0 ; j<App.partite.length ; j++){
				if(App.partite[j].isGiornatacorrente()){
					Partita partita = App.partite[j];
					nGiornata = App.partite[j].getGiornata();
					TableRow tableRow = new TableRow(rootView.getContext());
					
					TextView risultato = new TextView(rootView.getContext());
					risultato.setText(partita.getSquadracasa() +" "+partita.getGolsquadracasa() +"-"+ partita.getGolsquadratrasferta() +" "+partita.getSquadratrasferta());
					
					tableRow.addView(risultato);
					tableLayoutNew.addView(tableRow);
				}
			}
			giornata.setText("Giornata : " +  nGiornata);
			tableRowIntestazione.addView(giornata);
			tableLayoutIntestazione.addView(tableRowIntestazione);
			tableLayout.addView(tableLayoutIntestazione);
			tableLayout.addView(tableLayoutNew);
			TableRow tableRowVuta = new TableRow(rootView.getContext());
			TextView vuora = new TextView(rootView.getContext());
			vuora.setText(" ");
			tableRowVuta.addView(vuora);
			tableLayout.addView(tableRowVuta);
		//}	
		return rootView;
	}
	
	/**
	 * Represents an asynchronous login/registration task used to authenticate
	 * the user.
	 */
	public class CalendarioTask extends AsyncTask<Void, Void, Boolean> {
		@Override
		protected Boolean doInBackground(Void... params) {
			JSONObject holder = new JSONObject();
			DefaultHttpClient httpclient = new DefaultHttpClient();
			ObjectMapper mapper = new ObjectMapper();
			HttpPost httpost = new HttpPost(Constants.URL_REST + "getGiornataLega");
			try {
				
				holder.put(new String("username"), App.utente.getNome());
				holder.put(new String("password"), App.utente.getPassword());
				holder.put(new String("idlega"), App.utente.getIdlegaprivata());
				StringEntity se = new StringEntity(holder.toString());

				httpost.setEntity(se);
				
				httpost.setHeader("Accept", "application/json");
				httpost.setHeader("Content-type", "application/json");
				HttpResponse response = httpclient.execute(httpost);
				HttpEntity httpEntity = response.getEntity();
				InputStream is = httpEntity.getContent();
				App.partite = mapper.readValue(is, Partita[].class);
			} catch (JSONException e) {
				e.printStackTrace();
				return false;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return false;
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			} 

			return true;
		}
		
		@Override
		protected void onPostExecute(final Boolean success) {
		}

		@Override
		protected void onCancelled() {
		
		}
	}
}
